package com.wechat.msg;

import java.io.Serializable;

import com.swak.utils.JaxbMapper;
import com.swak.utils.StringUtils;

/**
 * 消息
 * 
 * @author 618lf
 */
public class Msg<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private String talker;
	private String nickName;

	public String getTalker() {
		return talker;
	}

	public T setTalker(String talker) {
		this.talker = talker;
		return as();
	}

	public String getNickName() {
		return nickName;
	}

	public T setNickName(String nickName) {
		this.nickName = nickName;
		return as();
	}

	/**
	 * 强转为当前对象
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected T as() {
		return (T) this;
	}

	/**
	 * 转为 XML 存储
	 * 
	 * @return
	 */
	@Override
	public String toString() {
		return JaxbMapper.toXml(this);
	}

	public String toXml() {
		return JaxbMapper.toXml(this);
	}

	public static <T> T fromXml(String xml, Class<T> clazz) {
		if (StringUtils.isBlank(xml) || clazz == null) {
			return null;
		}
		return JaxbMapper.fromXml(xml, clazz);
	}
}
